import {
  Component,
  ViewChild,
  Output,
  Injector,
  EventEmitter,
} from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
  PipelineNodeServiceProxy,
  PipelineServiceProxy,
  StationDto,
  ProcViewDto,
  PipelineListDto,
  ProcAndActViewDto,
} from "@shared/service-proxies/service-proxies";
import { ModalDirective } from "ngx-bootstrap";
import { finalize } from "rxjs/operators";
import { LazyLoadEvent } from "primeng/primeng";
import { Paginator } from "primeng/components/paginator/paginator";
import { StepsComponent } from "@app/pipeline/step/step.component";

@Component({
  selector: "quickGxzxModal",
  templateUrl: "./quick-gxzx-modal.component.html",
  styleUrls: ["./quick-gxzx-modal.component.less"],
})
export class QuickGxzxModalComponent extends AppComponentBase {
  @ViewChild("actionModal") modal: ModalDirective;
  @ViewChild("paginator") paginator: Paginator;
  @ViewChild("stepsComponent") stepsComponent: StepsComponent;

  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
  @Output() modalRun: EventEmitter<any> = new EventEmitter<any>();
  active = false;
  saving = false;
  runing = false;
  pipelineid: string;
  //modal 大小
  modalSize = "modal-xxl";
  //是否全屏
  isFullScreen = false;
  stationId = 0;
  station = new StationDto();
  stations = new Array<StationDto>();
  ipStations = new Array<StationDto>();
  procInfo = new ProcViewDto();
  pipeline: PipelineListDto = new PipelineListDto();
  procAndActView = new Array<ProcAndActViewDto>();
  ip = "";
  isShowAllStations = false;
  actCode = "KSBG";
  event: LazyLoadEvent;
  checked=false;
  constructor(
    injector: Injector,
    private _taskServiceProxy: PipelineNodeServiceProxy,
    private _pipelineService: PipelineServiceProxy
  ) {
    super(injector);
  }
  show(pipe: PipelineListDto, event?: LazyLoadEvent): void {
    this.pipelineid = pipe.id;
    this.pipeline = pipe;
    this.event = event;
    this.active = true;
    this.isFullScreen = false;
    if(this.pipeline.taskTypeName=="自制外协任务" || this.pipeline.taskTypeName=="外协任务"){
      this.checked=true;
  }
    this.reloadProcInfo();
    this.modal.show();
    //console.log(this.pipe);
  }
  fullScreen() {
    this.isFullScreen = !this.isFullScreen;
    setTimeout(() => {
      if (this.isFullScreen) {
        mApp.initScroller($(".m-scrollable-full-screen"), { height: "100%" });
      } else {
        $(".full-screen-content .mCSB_scrollTools").hide();
        $(".full-screen-content .mCustomScrollBox").removeClass(
          "mCustomScrollBox"
        );
        $(".full-screen-content .mCSB_container").removeClass("mCSB_container");
      }
    }, 0);
  }

  close(): void {
    //this.stepsComponent.getPipes();
    this.active = false;
    this.modal.hide();
    this.modalSave.emit(this.event);
  }
  reloadProcInfo() {
    this._taskServiceProxy.getProcInfo(this.pipelineid).subscribe((result) => {
      this.procInfo = result;
    });
    this._pipelineService
      .getProcListInfo(this.pipelineid)
      .subscribe((result) => {
        this.procAndActView = result;
        //console.log(this.procAndActView);
      });
  }
  selectChange() {
    this.ipStations.forEach((el) => {
      // tslint:disable-next-line:triple-equals
      if (this.stationId == el.id) {
        this.station = el;
      }
    });
  }
  checkboxChange() {
    this.stationId = 0;
    this.station = new StationDto();
    if (this.isShowAllStations) {
      this.ipStations = this.stations;
    } else {
      this.ipStations = this.stations.filter((el) => el.ip === this.ip);
    }
  }

  start() {
    this._taskServiceProxy
      .componentProcStart(this.pipelineid)
      .subscribe((result) => {
        this.notify.success("工序开始成功");
        this.reloadProcInfo();
      });
  }
  pause() {
    this._taskServiceProxy
      .componentProcPause(this.pipelineid)
      .subscribe((result) => {
        this.notify.success("工序暂停成功");
        this.reloadProcInfo();
      });
  }

  finish() {
    this._taskServiceProxy
      .componentProcFinish(this.pipelineid,this.pipeline.nodeId,this.checked)
      .subscribe((result) => {
        this.notify.success("工序完成成功");
        this.reloadProcInfo();
      });
  }

  restart() {
    this._taskServiceProxy
      .componentProcReInit(this.pipelineid)
      .subscribe((result) => {
        this.notify.success("工序重置成功");
        this.reloadProcInfo();
      });
  }

  run(): void {
    this.runing = true;

    this._pipelineService
      .getPipelineAndRun(this.pipelineid)
      .pipe(
        finalize(() => {
          this.runing = false;
        })
      )
      .subscribe(() => {
        this.notify.info("运行成功");
        this.reloadProcInfo();
        this.modalRun.emit(this.event);
      });
  }
}
